package com.gangwangchina.android.contactlib.iterators;

import java.util.Iterator;

import com.gangwangchina.android.contactlib.model.PersonContact;

import android.database.Cursor;
import android.util.Log;


public class PersonContactIterator implements Iterator<PersonContact>{

	private Cursor c;
	private boolean bIsHasData = true;
	private boolean bVisitFirstData =  false;
	
	/**
	 * Creates a PersonContactIterator over a Cursor
	 * The Cursor's columns should be [id, personContactId, accountName, accountType]
	 * @param c
	 */
	public PersonContactIterator(Cursor c){
		this.c = c;
		bIsHasData = c.moveToFirst();
	}
	
	public boolean isHasData()
	{
		return this.bIsHasData;
	}
	
	public boolean hasNext() {
		return !(c.isLast()||c.isAfterLast());
	}

	public PersonContact next() {
		if(bVisitFirstData)
		{
			c.moveToNext();
		}		
		bVisitFirstData = true;
		return currentPersonContact();
	}
	
	/**
	 * Returns the previous object and moves back the iterator
	 * @return the previous object
	 */
	public PersonContact previous(){
		c.moveToPrevious();
		return currentPersonContact();
	}

	public void remove() {
		Log.e("PersonContactItertaor", "Remove doesn't have any effect on a PersonContactIterator");
	}
	
	/**
	 * This method closes the Cursor used to retrieves data from the database
	 * Therefore, it should be called whenever the PersonIterator is not needed anymore
	 */
	public void close(){
		c.close();
	}
	
	public PersonContact currentPersonContact(){
		PersonContact pc = PersonContact.recoverPersonContact(c.getLong(1), c.getString(2), c.getString(3));
		pc.id = c.getLong(0);
		return pc;
	}

}
